import { Location, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { Component, OnInit } from '@angular/core';
// import { ToastService, UploadConfig } from 'ng-tui';
import { GlobalService, AuthService } from '../../../../cores/services';
import { PurchaseService } from '../../services/purchase.service';
import { Manage } from '../../interfaces/purchase.interface';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { Goods } from '../../interfaces/purchase.interface';
import { skipWhile, switchMap } from 'rxjs/operators';

@Component({
    templateUrl: './order.component.html',
    providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],
})
export class OrderComponent implements OnInit {
    theads = ['#', '订单编号', '采购单位', '采购员', '合同', '订购日期', '到货日期', '供应商名称',
        '采购金额(含税)', '采购金额(未税)', '预入仓库', '运输公司', '收货地址', '审核人', '单据状态', '录入人', '操作'];
    billTh = ['#', '明细编号', '产品名称', '条形码', '计量单位', '规格/型号', '口味/颜色', '采购数量', '采购箱数', '含税单价', '含税金额',
        '未税单价', '未税金额', '税率', '保质期(天)', '标准含税单价', '折扣率', '箱条码', '厂家货号', '品牌', '备注', '操作'];
    orderTh = ['#', '商品编号', '商品名称', '条形码', '厂家货号', '单位', '口味/颜色', '规格/尺寸', '数量', '箱数', '价格', '金额', '税率', '备注', '箱条码', '品牌', '操作'];

    search = new SearchParams({ start: '', end: '' });

    list = [];
    detailList = [];
    loading = false;
    idDetail: any = '';
    isSelect = false;
    parentId = '';
    pagination = new Pagination();
    parentTypes = new Array<Item>();
    childTypes = new Array<any>();
    goodsThreeType = new Array<any>();
    goods = { id: 0, isActive: 1, goodsName: '', isSelect: false, goodsParentType: '' };


    get getChildTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    get getThreeTypes(): Item[] {
        return this.childTypes.filter(type => type.parentId === this.goods.goodsParentType);
    }
    constructor(
        private activatedRoute: ActivatedRoute,
        private router: Router,
        private confirm: ConfirmService,
        private toast: ToastService,
        public global: GlobalService,
        private purchaseService: PurchaseService
    ) { }

    ngOnInit() {
        this.activatedRoute.url
            .pipe(skipWhile(() => this.router.url !== '/purchase/order'))
            .subscribe(() => {
                this.loadDatas(() => {
                    this.idDetail = this.list[0].id;
                    this.detailLoad(this.idDetail);
                });
            });
    }
    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }

    doReset() {
        this.search.clean();
        this.doSearch();
    }
    /**
 * order查询商品列表
 */
    loadDatas(complete = new Function) {
        this.loading = true;
        this.purchaseService.searchGoods(this.pagination, this.search)
            .subscribe({
                next: res => {
                    this.pagination.total = res.datas.total;
                    res.datas.rows.forEach(item => {
                        item['isSelect'] = this.isSelect;
                    });
                    this.list = res.datas.rows;
                    complete();
                },
                complete: () => this.loading = false
            });
    }
    /**
* order查询商品详情
*/
    detailLoad(detailId: number) {
        this.loading = true;
        this.idDetail = detailId;
        this.purchaseService.getDetail(this.idDetail)
            .subscribe({
                next: res => {
                    this.detailList = res.datas.goodsMsg;
                    this.parentId = res.datas.id;
                    this.list.forEach((v, i) => {
                        if (v.id === this.idDetail) {
                            this.list[i].isSelect = true;
                        } else {
                            this.list[i].isSelect = false;
                        }
                    });

                },
                complete: () => this.loading = false
            });
    }

    // confirmDelete(goods: Goods) {
    //     this.confirm.danger('删除确认', `您确认删除商品'${goods.goodsName}'吗？`)
    //         .pipe(switchMap<void, ApiData>(() => this.purchaseService.deleteGoods(goods.id)))
    //         .subscribe(() => {
    //             this.list.splice(this.list.indexOf(goods), 1);
    //             this.toast.success('删除成功', `成功删除商品'${goods.goodsName}`);
    //             this.loadDatas();
    //         });
    // }

}
